﻿// ***** Guidance generated code © Catharsis ***** //
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Web.Mvc;

using Castle.DynamicProxy;

using $prjsn$.Entity;

using ProjectBase.Core;
using ProjectBase.Core.Tools.Import;

namespace $safeprojectname$
{
    [Serializable]
    public class TraceableFilter : IInterceptor
    {
        #region AOP
        public virtual void Intercept(IInvocation invocation)
        {
            if (!DoApplyFilter(invocation))
            {
                invocation.Proceed();
                return;
            }

            // pre
            BeforeExecuting(invocation);

            // BindEntity
            invocation.Proceed();

        }

        protected virtual bool DoApplyFilter(IInvocation invocation)
        {
            // not of our interest
            if (invocation.Arguments.IsEmpty()) // first must be (entity) in place
            {
                return false;
            }

            // is it IAuditable?
            return invocation.Arguments[0] is ITraceable;
        }

        protected virtual void BeforeExecuting(IInvocation invocation)
        {
            var entity = invocation.Arguments[0] as ITraceable;
            entity.DateModified = DateTime.Now;
            if (invocation.Method.Name.IsEqual("Add"))
            {
                entity.DateCreated = DateTime.Now;
            }
        }
        #endregion AOP

        #region Tracable

        #endregion Tracable
    }
}